package pojo;

import java.util.LinkedHashMap;
import java.util.Map;

//ai 1.0版本
public class AI1 extends AI {
    private double k;
    public AI1(int N, int color, int chess,double k) {//初始化
        super.N=N;
        super.color = color;
        super.chess=chess;
        this.k=k;
        buildNext();
    }
    public int run(int[][] map) {//计算下次下的坐标x,y,返回值为当前得分
        this.map=map;
        x=N/2;
        y=N/2;
        int score=changeMap()?evaluateScore(color):0;
        recoverMap();
        return score;
    }
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    private int evaluateScore(int color) {//更新得分,并且返回当前得分
        int max=Integer.MIN_VALUE;
        boolean[][] eval=new boolean[N][N];
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                if(map[i][j]!=0){
                    int m=Math.min(i+2,N-1);
                    int m2=Math.min(j+2,N-1);
                    for(int k=Math.max(0,i-2);k<=m;k++){
                        for(int l=Math.max(0,j-2);l<=m2;l++){
                            if(map[k][l]==0&&!eval[k][l]){
                                //计算进攻得分
                                map[k][l]=color;
                                int attack=(int)(this.k*(rowEval(k,l,color)+colEval(k,l,color)+leftEval(k,l,color)+rightEval(k,l,color)));
                                //计算防守得分
                                map[k][l]=-color;
                                int defend=rowEval(k,l,-color)+colEval(k,l,-color)+leftEval(k,l,-color)+rightEval(k,l,-color);
                                map[k][l]=0;
                                int sum=attack+defend;
                                if(max<sum){
                                    max=sum;
                                    x=k;
                                    y=l;
                                }
                                eval[k][l]=true;
                            }
                        }
                    }
                }
            }
        }
        return max;
    }
    private int rowEval(int x, int y, int color) {//竖直方向
        StringBuilder sb=new StringBuilder();
        int m=Math.min(N-1,x+4);
        for(int i=Math.max(0,x-4);i<=m;i++){
            if(map[i][y]==color){
                sb.append('1');
            }else if(map[i][y]==0){
                sb.append('0');
            }else{
                sb.append('2');
            }
        }
        return getScore(sb.toString());
    }

    private int colEval(int x, int y, int color) {//水平方向
        StringBuilder sb=new StringBuilder();
        int m=Math.min(N-1,y+4);
        for(int i=Math.max(0,y-4);i<=m;i++){
            if(map[x][i]==color){
                sb.append('1');
            }else if(map[x][i]==0){
                sb.append('0');
            }else{
                sb.append('2');
            }
        }
        return getScore(sb.toString());
    }
    private int leftEval(int x,int y,int color){//左斜
        StringBuilder sb=new StringBuilder();
        int left=Math.max(-4,Math.max(-x,y-N+1));
        int right=Math.min(4,Math.min(N-1-x,y));
        for(int i=left;i<=right;i++){
            if(map[x+i][y-i]==color){
                sb.append('1');
            }else if(map[x+i][y-i]==0){
                sb.append('0');
            }else{
                sb.append('2');
            }
        }
        return getScore(sb.toString());
    }
    private int rightEval(int x,int y,int color){//右斜
        StringBuilder sb=new StringBuilder();
        int left=Math.max(-4,Math.max(-x,-y));
        int right=Math.min(4,Math.min(N-1-y,N-1-x));
        for(int i=left;i<=right;i++){
            if(map[x+i][y+i]==color){
                sb.append('1');
            }else if(map[x+i][y+i]==0){
                sb.append('0');
            }else{
                sb.append('2');
            }
        }
        return super.getScore(sb.toString());
    }
}
